﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace FSAPI
{
    public class RemoteServer
    {
        Thread t = null;
        public Ice.Communicator communtor_RecFromServer = null;//接收通信器

        //提供从机可执行的操作（当前项目采用抛出事件的方式让其他模块来执行具体的任务）
        public InspectionDisp InspectionInstance;

        private string  ipAddress;
        private int port;
        public string ID { get; set; }
        public string Name { get; set; }
        public string Desc { get; set; }
        public enum ErrorType
        {
            OK=0,
            ConnectError =1,
        }
        public RemoteServer(string IP, int Port)
        {
            ipAddress = IP;
            port=Port;
            ID = string.Format("{0}_{1}", IP, Port);
        }

        public ErrorType StartServerStatus = ErrorType.ConnectError;
        public ErrorType StartServer()
        {
            t = new Thread(() =>
            {
                try
                {
                    string EndpointName = "RemoteServer";
                    Ice.Properties properties = Ice.Util.createProperties();
                    properties.setProperty("Ice.MessageSizeMax", "200000000");
                    properties.setProperty("Ice.ThreadPool.Server.Size", "2");
                    //properties.setProperty("Ice.UDP.SndSize", "200000000");
                    //properties.setProperty(" Ice.UDP.RcvSize", "200000000");
                    Ice.InitializationData initData = new Ice.InitializationData();
                    initData.properties = properties;
                    communtor_RecFromServer = Ice.Util.initialize(initData);
                    Ice.ObjectAdapter adpter = communtor_RecFromServer.createObjectAdapterWithEndpoints(EndpointName, string.Format("default -h {0} -p {1}", ipAddress, port.ToString())); //communtor.createObjectAdapter("Printer");//3.创建适配器对象，参数为(适配器名称，缺省TCP/IP,端口号)                                             
                    InspectionInstance = new InspectionDisp();
                    adpter.add(InspectionInstance, Ice.Util.stringToIdentity(EndpointName + "Inspection"));//5.在适配器中传入对象，以及适配器名称。（接受者可能有多个）
                    adpter.activate();//激活适配器，适配器创建完毕之后处于holding
                    StartServerStatus = ErrorType.OK;
                    communtor_RecFromServer.waitForShutdown();//挂起发出调用的线程，直到服务器实现终止
                }
                catch (Exception e)
                {
                    StartServerStatus = ErrorType.ConnectError;
                }

                try
                {
                    communtor_RecFromServer.destroy();
                }
                catch (Exception e)
                {
                }
            });           
            t.Start();
            return ErrorType.OK;
        }

        public void CloseServer()
        {
            if (communtor_RecFromServer != null)
            {
                communtor_RecFromServer.shutdown();
            }
        }
        public void Destroy()
        {
            if (communtor_RecFromServer != null)
            {
                communtor_RecFromServer.destroy ();
            }
        }

    }
}
